
using System;
using System.Collections.Generic;

using Grr.Web;
using Grr.Models;

using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Views.Aspx;

public partial class Views_Grurrah_explore : GrurrahViewPage {

    private static readonly int MaxNumberResults = 5;

    private List<Tag> tags;
    private SearchResult popular_result;
    private SearchResult recent_result;

    protected override string TabName {
        get { return "explore"; }
    }

    public string ListTags
    {
        get { return (string) ViewState["Tags"]; }
    }

    public List<Tag> Tags
    {
        get { return tags; }
    }

    public SearchResult Popular
    {
        get {
            if (popular_result == null)
                return SearchResult.Empty;
            return popular_result; 
        }
    }

    public SearchResult Recent
    {
        get {
            if (recent_result == null)
                return SearchResult.Empty;
            return recent_result; 
        }
    }

    public int CurrentPopularResultsIndex
    {
        get
        {
            object o = ViewState["CurrentPopularResultsIndex"];
            if (o != null)
                return (int)o;
            return 0;
        }
        set
        {
            ViewState["CurrentPopularResultsIndex"] = value;
        }
    }

    public int CurrentRecentResultsIndex
    {
        get
        {
            object o = ViewState["CurrentRecentResultsIndex"];
            if (o != null)
                return (int)o;
            return 0;
        }
        set
        {
            ViewState["CurrentRecentResultsIndex"] = value;
        }
    }

    protected override void SetProperties()
    {
        tags = (List<Tag>)PropertyBag ["TagsList"];
        popular_result = (SearchResult)PropertyBag["Popular"];
        recent_result = (SearchResult)PropertyBag["Recent"];

        PopularNextButton.Visible = Popular.MoreMatchesAvailable;
        RecentNextButton.Visible = Recent.MoreMatchesAvailable;

        tag_cloud.Tags = Tags;
    }

    protected void PopularNextButton_Click(object sender, EventArgs e)
    {
        popular_result = Explore.GetPopularResults(MaxNumberResults, CurrentPopularResultsIndex + MaxNumberResults);
        CurrentPopularResultsIndex += MaxNumberResults;
        PopularPanel.ForceAnUpdate();

        PopularPrevButton.Visible = true;
        PopularNextButton.Visible = Popular.MoreMatchesAvailable;
    }

    protected void PopularPrevButton_Click(object sender, EventArgs e)
    {
        popular_result = Explore.GetPopularResults(MaxNumberResults, CurrentPopularResultsIndex - MaxNumberResults);
        CurrentPopularResultsIndex -= MaxNumberResults;
        PopularPanel.ForceAnUpdate();

        PopularNextButton.Visible = true;
        PopularPrevButton.Visible = CurrentPopularResultsIndex != 0;
    }

    protected void RecentNextButton_Click(object sender, EventArgs e)
    {
        recent_result = Explore.GetRecentResults(MaxNumberResults, CurrentRecentResultsIndex + MaxNumberResults);
        CurrentRecentResultsIndex += MaxNumberResults;
        RecentPanel.ForceAnUpdate();

        RecentPrevButton.Visible = true;
        RecentNextButton.Visible = Recent.MoreMatchesAvailable;
    }
    protected void RecentPrevButton_Click(object sender, EventArgs e)
    {
        recent_result = Explore.GetRecentResults(MaxNumberResults, CurrentRecentResultsIndex - MaxNumberResults);
        CurrentRecentResultsIndex -= MaxNumberResults;
        RecentPanel.ForceAnUpdate();

        RecentNextButton.Visible = true;
        RecentPrevButton.Visible = CurrentRecentResultsIndex != 0;
    }
}
